package com.jxx.androidstudy.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.AndroidException;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jxx.androidstudy.R;
import com.jxx.androidstudy.adapters.FruitAdapter;
import com.jxx.androidstudy.adapters.FruitRecyclerAdapter;
import com.jxx.androidstudy.bean.Fruit;

import java.util.ArrayList;

public class WidgetActivity extends AppCompatActivity {


    private Button btn_alert;

    private String[] data = {"Apple","Banna","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango"};
    private ListView listView;
    private ArrayList<Fruit> fruits = new ArrayList<>();
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_widget);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        btn_alert = findViewById(R.id.btn_alert);
        btn_alert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(WidgetActivity.this);
                dialog.setTitle("警告");
                dialog.setMessage("是否确定输入");
                dialog.setCancelable(true);
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(WidgetActivity.this,"OK Click",Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(WidgetActivity.this,"Cancels Click",Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.show();
            }
        });

        initFruits();
        listView  = findViewById(R.id.list_view);
        FruitAdapter fruitAdapter = new FruitAdapter(WidgetActivity.this,R.layout.fruit_item,fruits); //适配器模式
        listView.setAdapter(fruitAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Fruit fruit = fruits.get(i);
                Toast.makeText(WidgetActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });

        recyclerView = findViewById(R.id.recyclerView);

//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        recyclerView.setLayoutManager(linearLayoutManager);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);

        recyclerView.setLayoutManager(gridLayoutManager);
        FruitRecyclerAdapter fruitRecyclerAdapter = new FruitRecyclerAdapter(fruits);
        recyclerView.setAdapter(fruitRecyclerAdapter);
    }

    private void initFruits(){

        Fruit fruit = new Fruit("apple",R.drawable.logo);
        fruits.add(fruit);
        Fruit fruit1 = new Fruit("banna",R.drawable.logo);
        fruits.add(fruit1);
        Fruit fruit2 = new Fruit("orange",R.drawable.logo);
        fruits.add(fruit2);
        Fruit fruit3 = new Fruit("pear",R.drawable.logo);
        fruits.add(fruit3);
        Fruit fruit4 = new Fruit("grape",R.drawable.logo);
        fruits.add(fruit4);
        Fruit fruit5 = new Fruit("Cherry",R.drawable.logo);
        fruits.add(fruit5);
        Fruit fruit6 = new Fruit("Mango",R.drawable.logo);
        fruits.add(fruit6);
        Fruit fruit7 = new Fruit("stawberry",R.drawable.logo);
        fruits.add(fruit7);
        Fruit fruit8 = new Fruit("watermelon",R.drawable.logo);
        fruits.add(fruit8);
    }
}